const fs = require('fs');
const path = require('path');
const iconv = require('iconv-lite');

function testPhoneticExtraction() {
  const testFile = path.join(__dirname, '..', 'resources', 'dic', 'a', 'abandon.txt');
  
  console.log(`测试文件: ${testFile}`);
  
  try {
    // 使用iconv-lite读取GBK编码的文件
    const buffer = fs.readFileSync(testFile);
    const content = iconv.decode(buffer, 'gbk');
    
    console.log('文件内容（前1000字符）:');
    console.log(content.substring(0, 1000));
    console.log('\n' + '='.repeat(50) + '\n');
    
    // 尝试不同的正则表达式
    
    console.log('尝试匹配 <词典音标> 标签:');
    const dictPhoneticMatch1 = content.match(/<词典音标>([^<]+)<\/词典音标>/);
    console.log('结果1:', dictPhoneticMatch1);
    
    console.log('\n尝试匹配 CDATA 内容:');
    const dictPhoneticMatch2 = content.match(/<词典音标><!\$$CDATA\$$([^\]]+)$$]]><\/词典音标>/);
    console.log('结果2:', dictPhoneticMatch2);
    
    console.log('\n尝试匹配带空白字符的 CDATA 内容:');
    const dictPhoneticMatch3 = content.match(/<词典音标>\s*<!$$CDATA$$([^\]]+)$$]]>\s*<\/词典音标>/);
    console.log('结果3:', dictPhoneticMatch3);
    
    console.log('\n查找所有标签:');
    const allTags = content.match(/<[^>]+>/g);
    if (allTags) {
      const phoneticTags = allTags.filter(tag => tag.includes('音标'));
      console.log('包含"音标"的标签:');
      phoneticTags.forEach(tag => console.log('  ' + tag));
    }
    
    console.log('\n查找所有CDATA:');
    const allCDATA = content.match(/<!$$CDATA$$([^]]+)$$]]>/g);
    if (allCDATA) {
      console.log('CDATA 内容:');
      allCDATA.forEach((cdata, index) => {
        console.log(`  ${index + 1}. ${cdata}`);
      });
    }
    
  } catch (error) {
    console.error(`处理文件时出错:`, error.message);
  }
}

testPhoneticExtraction();